package maiyatian.videolive.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.IActivity;
import maiyatian.videolive.R;
import maiyatian.videolive.fragment.personCenter.ContributeFragment;
import maiyatian.videolive.fragment.personCenter.MyLiveFragment;
import maiyatian.videolive.fragment.personCenter.WhisperFragment;
import maiyatian.videolive.fragment.personHome.LiveInfoFragment;
import maiyatian.videolive.fragment.personHome.PersoninfoFragment;

/**
 * Created by 王中阳 on 2016/5/10.
 */
@ContentView(R.layout.activity_person_home)
public class PersonHomeActivity extends IActivity implements View.OnClickListener, TabLayout.OnTabSelectedListener {
    public static final String TAG = "PersonHomeActivity";
    @ViewInject(R.id.back)
    ImageView back;
    @ViewInject(R.id.ivLive)
    ImageView ivLive;
    @ViewInject(R.id.vpPersonIntro)
    ViewPager viewPager;
    @ViewInject(R.id.tabPersonIntro)
    TabLayout tabLayout;
    @ViewInject(R.id.addFocus)
    TextView addFocus;
    @ViewInject(R.id.whisper)
    TextView whisper;
    @ViewInject(R.id.toBlack)
    TextView toBlack;

    private Fragment personInfoFragment, liveFragment, contributeFragment;
    private List<Fragment> fragments;
    private List<String> tabTitles;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_home);
        x.view().inject(this);
        initviews();
        initTablayout();

    }

    private void initviews() {
        back.setOnClickListener(this);
        ivLive.setOnClickListener(this);
        addFocus.setOnClickListener(this);
        whisper.setOnClickListener(this);
        toBlack.setOnClickListener(this);
    }

    private void initTablayout() {
        tabLayout = (TabLayout) this.findViewById(R.id.tabPersonIntro);
        viewPager = (ViewPager) this.findViewById(R.id.vpPersonIntro);
        personInfoFragment = new PersoninfoFragment();
        liveFragment = new MyLiveFragment();
        contributeFragment = new ContributeFragment();
        fragments = new ArrayList<>();
        tabTitles = new ArrayList<>();
        fragments.add(personInfoFragment);
        fragments.add(liveFragment);
        fragments.add(contributeFragment);
        tabTitles.add(getResources().getString(R.string.info));
        tabTitles.add(getResources().getString(R.string.live));
        tabTitles.add(getResources().getString(R.string.contributeList));
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(2)));
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments, this);
        viewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        tabLayout.setupWithViewPager(viewPager);//将TabLayout和ViewPager关联起来。
        tabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
        if (viewPager != null) {
            setupViewPager(viewPager);
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setOnTabSelectedListener(this);
        }


    }

    private void setupViewPager(ViewPager viewPager) {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        personInfoFragment = new PersoninfoFragment();
        fragmentAdapter.addFragment(personInfoFragment, getString(R.string.info));
        liveFragment = new LiveInfoFragment();
        fragmentAdapter.addFragment(liveFragment, getString(R.string.live));
        contributeFragment = new ContributeFragment();
        fragmentAdapter.addFragment(contributeFragment, getString(R.string.contributeList));
        viewPager.setAdapter(fragmentAdapter);
    }


    //tablayout选中状态
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int pos = tab.getPosition();
        viewPager.setCurrentItem(pos, true);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    //切换
    static class FragmentAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

    //适配器
    class MyPagerAdapter extends FragmentStatePagerAdapter

    {
        private PersonHomeActivity activity;
        private List<Fragment> datas;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> datas, PersonHomeActivity activity) {
            super(fm);
            this.activity = activity;
            this.datas = datas;
        }

        @Override
        public Fragment getItem(int position) {
            return datas.get(position);
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles.get(position);//页卡标题
        }

        ////添加页卡 初始化item
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//官方推荐写法
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
            case R.id.ivLive:
                intent.setClass(this, PersonCenterActivity.class);
                intent.putExtra(IActivity.PAGETO, MyLiveFragment.TAG);
                this.startActivity(intent);
                break;
            case R.id.addFocus:
                showToast(this, "关注");
                break;
            case R.id.whisper:
                intent.setClass(this, PersonCenterActivity.class);
                intent.putExtra(IActivity.PAGETO, WhisperFragment.TAG);
                this.startActivity(intent);
                break;
            case R.id.toBlack:
                showToast(this, "拉黑");
                break;
        }
    }
}
